package shajie.shajun.mmhf.View.Fragment;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import java.util.List;

import shajie.shajun.mmhf.Adapter.MulRecyclerAdapter;
import shajie.shajun.mmhf.Adapter.Spinner_Adapter;
import shajie.shajun.mmhf.Base.BaseFragment;
import shajie.shajun.mmhf.Bean.Beans;
import shajie.shajun.mmhf.Bean2.SearchOrder;
import shajie.shajun.mmhf.Interface.RecyclerItemClickListener;
import shajie.shajun.mmhf.Public_Data;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.ViewModel.Bmobdata;
import shajie.shajun.mmhf.databinding.DialogOrderDetailBinding;
import shajie.shajun.mmhf.databinding.FragmentOrderSearchBinding;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2017/11/16.
 */

public class SearchOrder_Fragment extends BaseFragment<FragmentOrderSearchBinding> {
    private MulRecyclerAdapter mRecyclerAdapter;
    private FragmentOrderSearchBinding mBinding;
    private Beans<SearchOrder> mOrders;
    private int selectType=0;
    @Override
    public int setlayoutid() {
        return R.layout.fragment_order_search;
    }

    @Override
    public void init(FragmentOrderSearchBinding binding) {
       this.mBinding=binding;
        Public_Data.number++;
        GridLayoutManager layoutManager=new GridLayoutManager(getActivity(),1);
        mBinding.recyclerOrder.setLayoutManager(layoutManager);
        mBinding.imgSearchOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getorderdata(mBinding.editSearch.getText().toString());
            }
        });
        mBinding.imgSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        mBinding.recyclerOrder.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                View dialogView = null;
                LayoutInflater factory = LayoutInflater.from(getContext());//得到自定义对话框
                //dialogView = factory.inflate(R.layout.a_chakan_dialog, null);
                DialogOrderDetailBinding dialogOrderDetailBinding= DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.dialog_order_detail,null,false);
                dialogOrderDetailBinding.setOrder(mOrders.getBean().get(position));
                dialogOrderDetailBinding.setPosition(position+1);
                AlertDialog alertDialog;
                AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
                alertDialog=builder
                        .setView(dialogOrderDetailBinding.getRoot())
                        .create();
                alertDialog.show();
            }

            @Override
            public void onLongClick(View view, int posotion) {

            }
        }));
        Spinner_Adapter spinner_adapter=new Spinner_Adapter(getContext(),getContext().getResources().getStringArray(R.array.searchType));
        mBinding.spinnerSelect.setAdapter(spinner_adapter);
        mBinding.spinnerSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectType=position;

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }
    private void getorderdata(String s){
        mBinding.searchLoading.setVisibility(View.VISIBLE);
        mBinding.imgSearchOrder.setClickable(false);
        switch (selectType){
            case 0:
                Bmobdata.getsearchorderdata(getContext().getApplicationContext(),s,new Bmobdata.Singer<List<SearchOrder>>() {
                    @Override
                    public void success(List<SearchOrder> s) {
                        Log.e(TAG, "success: "+s.size() );
                        mOrders=new Beans<>(R.layout.item_search_order,s);
                        mBinding.searchLoading.setVisibility(View.GONE);
                        mBinding.imgSearchOrder.setClickable(true);
                        if (s.size()==0) {
                            toast("未找到相关数据");
                        }
                        mRecyclerAdapter =new MulRecyclerAdapter(getContext(),mOrders);
                        mBinding.recyclerOrder.setAdapter(mRecyclerAdapter);

                    }
                    @Override
                    public void faile(String s) {
                        mBinding.searchLoading.setVisibility(View.GONE);
                        mBinding.imgSearchOrder.setClickable(true);
                        toast(s);
                    }
                });
                break;
            case 1:
                Bmobdata.getSearchPerson(getContext().getApplicationContext(),s,new Bmobdata.Singer<List<SearchOrder>>() {
                    @Override
                    public void success(List<SearchOrder> s) {
                        Log.e(TAG, "success: "+s.size() );
                        mOrders=new Beans<>(R.layout.item_search_order,s);
                        mBinding.searchLoading.setVisibility(View.GONE);
                        mBinding.imgSearchOrder.setClickable(true);
                        if (s.size()==0) {
                            toast("未找到相关数据");
                        }
                        mRecyclerAdapter =new MulRecyclerAdapter(getContext(),mOrders);
                        mBinding.recyclerOrder.setAdapter(mRecyclerAdapter);

                    }
                    @Override
                    public void faile(String s) {
                        mBinding.searchLoading.setVisibility(View.GONE);
                        mBinding.imgSearchOrder.setClickable(true);
                        toast(s);
                    }
                });
                break;
        }


    }

    @Override
    public void onDestroy() {
        Public_Data.number--;
        super.onDestroy();
    }
}
